home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / icontopb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  3.6 KB  |  155 lines

  1. /* icontopbm.c - read a Sun icon file and produce a portable bitmap
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. static void ReadIconFile ARGS(( FILE* file, int* width, int* height, short** data ));
  16.  
  17. void
  18. main( argc, argv )
  19.     int argc;
  20.     char* argv[];
  21.     {
  22.     FILE* ifp;
  23.     bit* bitrow;
  24.     register bit* bP;
  25.     int rows, cols, row, col, shortcount, mask;
  26.     short* data;
  27.  
  28.     pbm_init( &argc, argv );
  29.  
  30.     if ( argc > 2 )
  31.     pm_usage( "[iconfile]" );
  32.  
  33.     if ( argc == 2 )
  34.     ifp = pm_openr( argv[1] );
  35.     else
  36.     ifp = stdin;
  37.  
  38.     ReadIconFile( ifp, &cols, &rows, &data );
  39.  
  40.     pm_close( ifp );
  41.  
  42.     pbm_writepbminit( stdout, cols, rows, 0 );
  43.     bitrow = pbm_allocrow( cols );
  44.  
  45.     for ( row = 0; row < rows; row++ )
  46.     {
  47.     shortcount = 0;
  48.     mask = 0x8000;
  49.     for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  50.         {
  51.         if ( shortcount >= 16 )
  52.         {
  53.         data++;
  54.         shortcount = 0;
  55.         mask = 0x8000;
  56.         }
  57.         *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
  58.         shortcount++;
  59.         mask = mask >> 1;
  60.         }
  61.     data++;
  62.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  63.     }
  64.  
  65.     pm_close( stdout );
  66.     exit( 0 );
  67.     }
  68.  
  69. /* size in bytes of a bitmap */
  70. #define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))
  71.  
  72. static void
  73. ReadIconFile( file, width, height, data )
  74.     FILE* file;
  75.     int* width;
  76.     int* height;
  77.     short** data;
  78.     {
  79.     char variable[81], ch;
  80.     int status, value, i, data_length, gotsome;
  81.  
  82.     gotsome = 0;
  83.     *width = *height = -1;
  84.     for ( ; ; )
  85.     {
  86.     while ( ( ch = getc( file ) ) == ',' || ch == '\n' || ch == '\t' ||
  87.         ch == ' ' )
  88.         ;
  89.     for ( i = 0;
  90.           ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && ch != ' ';
  91.           i++ )
  92.         {
  93.         variable[i] = ch;
  94.         ch = getc( file );
  95.         }
  96.     variable[i] = '\0';
  97.  
  98.     if ( strcmp( variable, "*/" ) == 0 && gotsome )
  99.         break;
  100.  
  101.     if ( fscanf( file, "%d", &value ) != 1 )
  102.         continue;
  103.  
  104.     if ( strcmp( variable, "Width" ) == 0 )
  105.         {
  106.         *width = value;
  107.         gotsome = 1;
  108.         }
  109.     else if ( strcmp( variable, "Height" ) == 0 )
  110.         {
  111.             *height = value;
  112.         gotsome = 1;
  113.         }
  114.     else if ( strcmp( variable, "Depth" ) == 0 )
  115.             {
  116.         if ( value != 1 )
  117.         pm_error( "invalid depth" );
  118.         gotsome = 1;
  119.         }
  120.     else if ( strcmp( variable, "Format_version" ) == 0 )
  121.             {
  122.         if ( value != 1 )
  123.         pm_error( "invalid Format_version" );
  124.         gotsome = 1;
  125.         }
  126.     else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
  127.             {
  128.         if ( value != 16 )
  129.         pm_error( "invalid Valid_bits_per_item" );
  130.         gotsome = 1;
  131.         }
  132.     }
  133.  
  134.     if ( *width <= 0 )
  135.     pm_error( "invalid width: %d", *width );
  136.     if ( *height <= 0 )
  137.     pm_error( "invalid height: %d", *height );
  138.  
  139.     data_length = BitmapSize( *width, *height );
  140.     *data = (short*) malloc( data_length );
  141.     if ( *data == NULL )
  142.         pm_error( "out of memory" );
  143.     data_length /= sizeof( short );
  144.     
  145.     for ( i = 0 ; i < data_length; i++ )
  146.     {
  147.     if ( i == 0 )
  148.         status = fscanf( file, " 0x%4hx", *data );
  149.     else
  150.         status = fscanf( file, ", 0x%4hx", *data + i );
  151.     if ( status != 1 )
  152.         pm_error( "error 4 scanning bits item" );
  153.         }
  154.     }
  155.